//
//  PictureTakingController.m
//  CalloutTest
//
//  Created by 范武 on 13-3-6.
//  Copyright (c) 2013年 福建邮科通讯服务有限公司. All rights reserved.
//

#import "PictureTakingController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "UIImage+Shrink.h"
#import "UIUtil.h"
#import "PatrolServiceClient.h"
@interface PictureTakingController ()
{
    UIImagePickerController * imagePicker;
    UIImageView * currentImageView;
}
@end

@implementation PictureTakingController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _imageArray = [NSMutableArray arrayWithCapacity:5];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UITapGestureRecognizer * tapReco1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(takePicture:)];
    [_imageView1 addGestureRecognizer:tapReco1];
    
    UITapGestureRecognizer * tapReco2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(takePicture:)];
    [_imageView2 addGestureRecognizer:tapReco2];
    
    UITapGestureRecognizer * tapReco3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(takePicture:)];
    [_imageView3 addGestureRecognizer:tapReco3];
    
    imagePicker = [UIImagePickerController new];
    imagePicker.delegate = self;
    
}





- (void) takePicture:(UITapGestureRecognizer *)reco
{
    if ([self doseCameraSupportShootingPhotos]) {
        currentImageView = (UIImageView *)reco.view;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }else{
        [UIUtil alertOKWithTitle:@"错误" withMsg:@"您正在使用的设备不支持拍照" withDelegate:nil];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Image Picker
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage* originImage = info[UIImagePickerControllerOriginalImage];
    UIImage * avata = [originImage shrinkToSize:CGSizeMake(57, 57)];
    UIImage * shrink = [originImage shrinkToSize:CGSizeMake(640, 960)];
    [_imageArray addObject:shrink];
    currentImageView.image = avata;
    currentImageView = nil;
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - camera detect
- (BOOL) isCameraAvailable
{
	return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}

- (BOOL) doseCameraSupportShootingPhotos
{
    
	return [self cameraSupportsMedia:(NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypeCamera];
}


- (BOOL) cameraSupportsMedia:(NSString *)paraMediaType sourceType:(UIImagePickerControllerSourceType) paramSourceType
{
	__block BOOL result = NO;
	
	NSArray * avaliableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:paramSourceType];
	[avaliableMediaTypes enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL * stop){
		NSString * mediaType = object;
		if ([mediaType isEqualToString:paraMediaType]) {
			result = YES;
			*stop = YES;
		}
	}];
	return result;
}

#pragma mark - upload Pics

- (NSString *)generatePicUploadNameWithTaskID:(NSInteger)taskID username:(NSString *) username picNumber:(NSInteger) num
{
    return [NSString stringWithFormat:@"%i-%@-%i.jpeg",taskID,username,num];
}

- (void)uploadPicturesWorkTask:(NSInteger)taskID username:(NSString *)username
{
    int i = 1;
    for (UIImage * img in _imageArray) {
        NSString * name = [self generatePicUploadNameWithTaskID:taskID username:username picNumber:i++];
        PatrolServiceClient * client = [PatrolServiceClient shareInstance];
        [client uploadPic:img name:name taskID:taskID];
    }
    return;
}

@end
